// ITI 1120 Winter 2012, Lab 5, Example 2
// Name: Gilbert Arbez, Student# 1234567

/**
 * This program finds the standard deviation of a set of values.
 */

class Lab5Ex2
{
    public static void main (String[] args)
    {
        // DECLARE VARIABLES/DATA DICTIONARY 

        double[] arrValues; // Array of real values for which to find average
        int n;              // Length of array of values
        double[] statsArray;  // Array of statistis computed from values

        // PRINT OUT IDENTIFICATION INFORMATION
        
        System.out.println();
        System.out.println("ITI 1120 Winter 2012, Lab 5, Example 2");
        System.out.println("Name: Gilbert Arbez, Student# 1234567");
        System.out.println();
       
       
        // Get data from user   
        System.out.println( "Please enter several real values on a line." );
        arrValues = ITI1120.readDoubleLine();        
        // Find value of n from array length        
        n = arrValues.length;

        // CALL problem solving algorithm
        statsArray = statistics(arrValues,n);
                                
        // PRINT OUT RESULTS AND MODIFIEDS
        System.out.println("The average is " + statsArray[0]);
        System.out.println("The maximum is " + statsArray[1]);
        System.out.println("The minimum is " + statsArray[2]);
    }//end main
        
     /* 
     * Method:  statistics
     * Description: Computes the average, maximum and minium of a set of 
     *              values contained in an array.
     * Givens: arr - array of real values
     *         n - number of elements in the array.
     */
    public static double[] statistics(double [] arr, int n)
    {
       // Results
        double[] stats;       // RESULT:        Standard deviation of all values in x

       // Intermediates
        double sum;         // Running total of array values 
        int index;          // Array position while calculating sum.
       
       // Body
        stats = new double[3]; // for recording average, maximum, minimum
        stats[0] = 0.0;
        stats[1] = arr[0];
        stats[2] = arr[0];
        index = 0;        
        while(index < n)
        {
          // Maximum
          if(arr[index] > stats[1])
          {
            stats[1] = arr[index];
          }
          else {/* do nothing */ }
          // Minimum
          if(arr[index] < stats[2])
          {
            stats[2] = arr[index];
          }
          else {/* do nothing */ }
          // Average
          stats[0] = stats[0] + arr[index];
          index = index + 1;
        }        
        stats[0] = stats[0]/n;

       // return the results
       return(stats);
    }

}//end class
